package com.kyzh.core.activities

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.Discuss
import com.gushenge.core.requests.UserRequest
import com.kyzh.core.R
import com.kyzh.core.adapters.BiaoqianAdapter
import com.kyzh.core.databinding.ItemMydiscussBinding
import com.kyzh.core.uis.alert
import kotlinx.android.synthetic.main.activity_mydiscuss.*

class MyDiscussActivity : BaseActivity() {

    private val beans = arrayListOf<Discuss>()
    private val adapter = Adapter(R.layout.item_mydiscuss,beans)
    private var p =1
    private var max =2
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mydiscuss)
        initView()
    }

    private fun initView() {
        rvList.adapter = adapter
        refresh()
        adapter.setEmptyView(R.layout.empty)
        adapter.loadMoreModule.isEnableLoadMore=true
        adapter.loadMoreModule.isEnableLoadMoreIfNotFullPage=false
        adapter.loadMoreModule.setOnLoadMoreListener {
            if (max<p){
                adapter.loadMoreModule.loadMoreEnd()
            }else{
                getData()
                adapter.loadMoreModule.loadMoreComplete()
            }
        }
        root.setOnRefreshListener {
            refresh()
            root.isRefreshing = false
        }
    }


    private inner class Adapter(layout:Int,beans:ArrayList<Discuss>):BaseQuickAdapter<Discuss,BaseDataBindingHolder<ItemMydiscussBinding>>(layout,beans), LoadMoreModule {
        override fun convert(holder: BaseDataBindingHolder<ItemMydiscussBinding>, item: Discuss) {
            holder.dataBinding?.let {
                it.data  =item
                it.bq.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                it.bq.adapter = BiaoqianAdapter( item.biaoqian)
                it.ivChosen.visibility = if (item.jiajing=="1") View.VISIBLE else View.GONE
                it.tvDelete.setOnClickListener {
                    alert(getString(R.string.deleteDiscuss),getString(R.string.sureToDelete),getString(R.string.sure),getString(R.string.cancel),{
                        UserRequest.myDiscussDelete(item.id){
                            beans.remove(item)
                            notifyDataSetChanged()
                        }
                    },{

                    })
                }
            }
        }

    }



    fun getData(){
        UserRequest.myDiscuss(p){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
    fun refresh(){
        UserRequest.myDiscuss(1){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.clear()
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
}